<?php

namespace App\Controller\Test;


use App\Controller\BaseController;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

class ValidatedController extends BaseController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;

    public function index(RequestInterface $request)
    {
        $rules = [
            'anchor_id' => 'required|phone_number:11',
            'from' => 'required',
            'version_code' => 'required',
            'channel' => 'required'
        ];
        $language = Context::get('language', 'en');
        if ($language == 'zh-zn') {
            $messages = [
                'required' => ':attribute 不能为空',
                'anchor_id.phone_number' => ':attribute 格式不对',
            ];
        } elseif ($language == 'zh-hk') {
            $messages = [
                'required' => 'The :attribute 不能為空',
                'anchor_id.phone_number' => ':attribute 格式不對',
            ];
        } else {
            $messages = [
                'required' => 'The :attribute field is required.',
                'anchor_id.phone_number' => 'The :attribute Format is incorrect.',
            ];
        }

        $validator = $this->validationFactory->make($request->all(), $rules, $messages);
        $validator->validate();
        // if ($validator->fails()){
        //     // Handle exception
        //     $errorMessage = $validator->errors()->first();
        //     return $this->return(formatData(['code' => 500, 'desc' => $errorMessage]));
        // }

    }
}